package backgammon;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JPanel;

/**
 *
 * @author Jacob Kwitkoski
 * @version 1.0
 */
public class GamePanel extends JPanel implements MouseListener,
                                                 MouseMotionListener {
    /**
     * <p>The array of polygons representing positions on the board.</p>
     */
    private Polygon[] positions;

    /**
     * <p>The color of the board.</p>
     */
    private final Color board = new Color(60, 100, 35);

    /**
     * <p>The color of the home positions.</p>
     */
    private final Color home = new Color(140, 65, 45);

    /**
     * <p>The color position one.</p>
     */
    private final Color pos1 = new Color(245, 222, 179);

    /**
     * <p>The color positions two.</p>
     */
    private final Color pos2 = new Color(140, 42, 45);

    /**
     * <p>Position of the mouse.</p>
     */
    private String positionName = "Current position";

    /**
     * <p>Constructs a panel for the game to be on.</p>
     */
    public GamePanel() {
        addMouseListener(this);
        addMouseMotionListener(this);
        arrangePositions();
    }

    @Override
    public final void paintComponent(final Graphics g) {
        super.paintComponent(g);
        this.setBackground(board);

        int length = positions.length;
        //fill the color of all the positions
        for (int i = 0; i < length; i++) {
            if (i < 2 || i > 25) {
                g.setColor(home);
            }
            else {
                if (i % 2 == 0) {
                    g.setColor(pos1);
                }
                else {
                    g.setColor(pos2);
                }
            }
            g.fillPolygon(positions[i]);
        }
        g.setColor(Color.BLACK);
        g.drawString(positionName, 725, 450);
    }

    /**
     * <p>Arranges the positions on the board.</p>
     */
    private void arrangePositions() {
        positions = new Polygon[28];
        int length = positions.length;
        for (int i = 0; i < length; i++) {
            positions[i] = new Polygon();
        }

        //initialize all position polygons
        int top = 0;
        int midTop = 220;
        int barTop = 230;
        int barBottom = 270;
        int midBottom = 280;
        int bottom = 500;

        //initialize top of board polygons
        int Tleft = 0;
        int Tmiddle = 25;
        int Tright = 50;

        for (int i = 14; i < length; i++) {
            if (i > 25) {//home or bar positions
                positions[i].addPoint(Tleft, top);
                positions[i].addPoint(Tright, top);
                positions[i].addPoint(Tright, barTop);
                positions[i].addPoint(Tleft, barTop);
            }
            else {//regular positions
                positions[i].addPoint(Tleft, top);
                positions[i].addPoint(Tright, top);
                positions[i].addPoint(Tmiddle, midTop);
            }
            if (i == 19) {//next one is the bar position
                Tleft += 100;
                Tmiddle += 100;
                Tright += 100;
            } else if (i == 26) {//reached home, go back and draw bar
                Tleft -= 350;
                Tmiddle -= 350;
                Tright -= 350;
            }
            else {//shift for next position
                Tleft += 50;
                Tmiddle += 50;
                Tright += 50;
            }
        }

        //initialize bottom of board polygons
        Tleft = 0;
        Tmiddle = 25;
        Tright = 50;

        for (int i = 13; i >= 0; i--) {
            if (i < 2) {//home or bar positions
                positions[i].addPoint(Tleft, bottom);
                positions[i].addPoint(Tright, bottom);
                positions[i].addPoint(Tright, barBottom);
                positions[i].addPoint(Tleft, barBottom);
            }
            else {//regular positions
                positions[i].addPoint(Tleft, bottom);
                positions[i].addPoint(Tright, bottom);
                positions[i].addPoint(Tmiddle, midBottom);
            }
            if (i == 8) {//next one is the bar position
                Tleft += 100;
                Tmiddle += 100;
                Tright += 100;
            }
            else if (i == 1) {//reached home, go back and draw bar
                Tleft -= 350;
                Tmiddle -= 350;
                Tright -= 350;
            }
            else {//shift for next position
                Tleft += 50;
                Tmiddle += 50;
                Tright += 50;
            }
        }
    }

    /**
     * <p>What to do when the mouse is clicked.</p>
     * @param event mouse event
     */
    public void mouseClicked(MouseEvent event) {
        repaint();
    }
    /**
     * <p>What to do when the mouse enters the frame.</p>
     * @param event mouse event
     */
    public void mouseEntered(MouseEvent event) { }
    /**
     * <p>What to do when the mouse exits the frame.</p>
     * @param event mouse event
     */
    public void mouseExited(MouseEvent event) { }
    /**
     * <p>What to do when the mouse is pressed.</p>
     * @param event mouse event
     */
    public void mousePressed(MouseEvent event) {
        repaint();
    }
    /**
     * <p>What to do when the mouse is released.</p>
     * @param event mouse event
     */
    public void mouseReleased(MouseEvent event) {
        repaint();
    }
    /**
     * <p>What to do when the mouse is dragged.</p>
     * @param event mouse event
     */
    public void mouseDragged(MouseEvent event) {
        repaint();
    }
    /**
     * <p>What to do when the mouse is moved.</p>
     * @param event mouse event
     */
    public void mouseMoved(MouseEvent event) {
        Point p = event.getPoint();
        positionName = "x = " + p.x + ", y = " + p.y;
        repaint();
    }
}
